import { defineConfig } from 'vitepress';
import mdContainer from 'markdown-it-container';
import path from 'node:path';
import fs from 'node:fs';
import vueJsx from '@vitejs/plugin-vue-jsx';

// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: ' iBiz 产品生命周期管理',
  description: 'iBiz 产品生命周期管理',
  lang: 'zh-CN',
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    lightModeSwitchTitle: '切换到浅色模式',
    darkModeSwitchTitle: '切换到深色模式',

    docFooter: {
      prev: '上一页',
      next: '下一页',
    },

    outlineTitle: '页面导航',

    socialLinks: [
      {
        icon: {
          svg: `
          <svg height="32" viewBox="0 0 32 32" width="32" xmlns="http://www.w3.org/2000/svg">
            <title>gitee</title>
            <g fill="none" fill-rule="evenodd">
            <circle cx="16" cy="16" fill="#c71d23" r="16"/>
            <path d="m24.0987698 14.2225144h-9.0863697c-.4362899.000207-.7900048.3538292-.790326.7901191l-.0005173 1.9752185c-.0003277.4363707.353328.7902117.7896987.790326.0000712 0 .0001424 0 .0002135-.0002135l5.5317648-.0000461c.4363708-.0000102.7901221.3537352.7901257.790106 0 .0000022 0 .0000044-.0000066.0000066v.1975077.1975318c0 1.3091122-1.0612451 2.3703573-2.3703573 2.3703573h-7.5067195c-.4363081-.0000218-.790009-.353713-.7900429-.7900211l-.0002069-7.5059917c-.0001014-1.3091122 1.0611145-2.3703865 2.3702267-2.3704226.0000217 0 .0000435 0 .0000653.0000653h11.0602463c.4361793-.0004902.7898484-.35394.7906091-.79011894l.0012251-1.97521881c.0007606-.43637034-.3527683-.79033806-.7891389-.79060871-.0001634-.0000001-.0003268-.00000015-.0004901.00048976h-11.0617654c-3.27278051 0-5.92589329 2.65311278-5.92589329 5.9258933v11.0612755c0 .4363707.35374837.7901191.7901191.7901191h11.65447149c2.9454379 0 5.3331872-2.3877493 5.3331872-5.3331872v-4.5430682c0-.4363707-.3537484-.7901191-.7901191-.7901191z" fill="#fff"/>
            </g>
          </svg>`,
        },
        link: 'https://gitee.com/iBizModeling/ibiz-data-view',
      },
    ],

    nav: [{ text: '指南', link: '/guide/getting-started' }],

    sidebar: [
      {
        text: '指南',
        items: [
          {
            text: '快速开始',
            link: '/guide/getting-started',
          },
          {
            text: '编辑器插件',
            collapsed: true,
            items: [
              {
                text: '数据选择（路由）',
                link: '/guide/plm/route-picker',
              },
              {
                text: '锚点富文本',
                link: '/guide/plm/ancho-html',
              },
              {
                text: '直接内容（活动）',
                link: '/guide/plm/raw-activity',
              },
              {
                text: '关注',
                link: '/guide/plm/attention',
              },
              {
                text: '人员选择',
                link: '/guide/plm/personel-select',
              },
              {
                text: '评论列表项',
                link: '/guide/plm/comment-item',
              },
              {
                text: '人员信息',
                link: '/guide/plm/personal-info',
              },
              {
                text: '模块选择',
                link: '/guide/plm/dynamic-tree-select',
              },
              {
                text: '数据选择（管理）',
                link: '/guide/plm/manage-mpicker',
              },
              {
                text: '选项框列表增强',
                link: '/guide/plm/checkbox-list-extend',
              },
              {
                text: '自动规则',
                link: '/guide/plm/auto-rules',
              },
              {
                text: '人员头像上传',
                link: '/guide/plm/avatar-upload',
              },
              {
                text: '评论回复列表',
                link: '/guide/plm/comment-list',
              },
              {
                text: '表情输入框',
                link: '/guide/plm/emoji-input',
              },
              {
                text: '过滤条件编辑器',
                link: '/guide/plm/filter-condition',
              },
              {
                text: '评论富文本',
                link: '/guide/plm/html-comment',
              },
              {
                text: '图标选择',
                link: '/guide/plm/icon-picker',
              },
              {
                text: 'json-schema',
                link: '/guide/plm/json-schema',
              },
              {
                text: 'luckysheet编辑插件',
                link: '/guide/plm/lucky-sheet-editor',
              },
              {
                text: '阶段进度条',
                link: '/guide/plm/stage-progress-bar',
              },
              {
                text: '团队选择器',
                link: '/guide/plm/team-picker',
              },
              {
                text: '个人团队选择',
                link: '/guide/plm/team-select',
              },
              {
                text: '树选择',
                link: '/guide/plm/tree-picker',
              },
            ],
          },
          {
            text: '部件插件',
            collapsed: true,
            items: [
              {
                text: '自定义图表（标签）',
                link: '/guide/plm/custom-label',
              },
              {
                text: '过渡时间线',
                link: '/guide/plm/transition-time-line',
              },
              {
                text: '树导航栏（默认展开指定节点）',
                link: '/guide/plm/default-expand-tree-exp-bar',
              },
              {
                text: '步骤看板',
                link: '/guide/plm/steps-kanban',
              },
              {
                text: '分页导航下拉',
                link: '/guide/plm/tab-exp-select',
              },
              {
                text: '编号标题栏',
                link: '/guide/plm/identifier-title',
              },
              {
                text: '用例步骤',
                link: '/guide/plm/repeater-grid-case-steps',
              },
              {
                text: '看板快捷操作',
                link: '/guide/plm/kanban-operate',
              },
              {
                text: '工时表格',
                link: '/guide/plm/workload-table',
              },
              {
                text: '序号表格',
                link: '/guide/plm/number-grid',
              },
              {
                text: '附件表格',
                link: '/guide/plm/attachment-grid',
              },
              {
                text: '简单列表',
                link: '/guide/plm/sample-list',
              },
              {
                text: '分组树',
                link: '/guide/plm/group-tree',
              },
              {
                text: '列表树',
                link: '/guide/plm/list-tree',
              },
              {
                text: '报表详情',
                link: '/guide/plm/chart-grid',
              },
              {
                text: '自定义用户信息',
                link: '/guide/plm/custom-user-info',
              },
              {
                text: '基线比对表格插件',
                link: '/guide/plm/baseline-compare',
              },
              {
                text: '多选视图表格插件（多选框）',
                link: '/guide/plm/checkbox-grid',
              },
              {
                text: '增强导航栏',
                link: '/guide/plm/drbar-ex',
              },
              {
                text: '编辑表单数据对比',
                link: '/guide/plm/form-data-comparison',
              },
              {
                text: '资源甘特图',
                link: '/guide/plm/resource-gantt',
              },
              {
                text: '评审列表',
                link: '/guide/plm/review-list',
              },
              {
                text: '路由导航插件',
                link: '/guide/plm/route-drbar',
              },
              {
                text: '搜索栏过滤条件编辑器',
                link: '/guide/plm/search-bar-filter-condition',
              },
            ],
          },
          {
            text: '视图插件',
            collapsed: true,
            items: [
              {
                text: '分享检查页面',
                link: '/guide/plm/shared-check-page',
              },
            ],
          },
          {
            text: '表格列插件',
            collapsed: true,
            items: [
              {
                text: '表格列（支持获取编辑列）',
                link: '/guide/plm/grid-edit-column',
              },
              {
                text: '图标（表格列）',
                link: '/guide/plm/icon-grid-column',
              },
              {
                text: '剪切板',
                link: '/guide/plm/clip-board',
              },
              {
                text: '表格列变更版本',
                link: '/guide/plm/grid-change-version-column',
              },
              {
                text: '表格直接代码编辑列',
                link: '/guide/plm/grid-jscode-column',
              },
              {
                text: '表格列评审结果',
                link: '/guide/plm/grid-review-results-column',
              },
            ],
          },
          {
            text: '工具栏项插件',
            collapsed: true,
            items: [
              {
                text: '显示模式切换工具栏项',
                link: '/guide/plm/show-mode-toolbar-item',
              },
            ],
          },
          {
            text: '面板项插件',
            collapsed: true,
            items: [
              {
                text: '面包屑导航',
                link: '/guide/plm/breadcrumb-exp',
              },
              {
                text: '切换显示模式',
                link: '/guide/plm/switch-show-mode',
              },
            ],
          },
          {
            text: '行为插件',
            collapsed: true,
            items: [
              {
                text: '文件上传',
                link: '/guide/plm/file-upload',
              },
              {
                text: '文件下载',
                link: '/guide/plm/file-download',
              },
              {
                text: '修改密码',
                link: '/guide/plm/change-password',
              },
              {
                text: '评审内容排序',
                link: '/guide/plm/review-content-sort',
              },
            ],
          },
        ],
      },
    ],
  },
  markdown: {
    config: md => {
      md.use(mdContainer, 'demo', {
        validate(params: string) {
          return !!params.trim().match(/^demo\s*(.*)$/);
        },

        render(
          tokens: {
            nesting: number;
            info: string;
            content: string;
            type: string;
            children?: { content: string }[];
          }[],
          idx: number,
        ) {
          if (tokens[idx].nesting === 1) {
            const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
            const src = m && m.length > 1 ? m[1] : '';
            if (!src) {
              throw new Error(`未配置图片`);
            }
            const sourceFileToken = tokens[idx + 2];
            const sourceFile = sourceFileToken.children?.[0].content || '';
            let rawSource = '';
            if (sourceFileToken.type === 'inline' && sourceFile) {
              rawSource = fs.readFileSync(
                path.resolve('examples', sourceFile),
                'utf-8',
              );
            }
            if (!rawSource) {
              throw new Error(`未找到文件: ${sourceFile}`);
            }
            const splitArr = sourceFile.split('.');
            const ext = splitArr[splitArr.length - 1];
            const source = md.render(`\`\`\` ${ext}\n${rawSource}\n\`\`\``);
            return `<Demo src="${src}" rawSource="${encodeURIComponent(
              rawSource,
            )}" source="${encodeURIComponent(source)}" >`;
          } else {
            return '</Demo>';
          }
        },
      });
    },
  },
  vite: {
    plugins: [vueJsx()],
  },
});
